import 'package:daoyi_ui_admin_getx/app/widgets/common_image_widget.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:url_launcher/url_launcher.dart';

import 'home_recommend_data.dart';

final networkUriRef = RegExp("^https?://");
final localUriRef = RegExp("^/");

class HomeRecommendItemWidget extends StatelessWidget {
  final HomeRecommendData data;
  final double width;

  const HomeRecommendItemWidget(this.data, {super.key, this.width = 100});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        if (networkUriRef.hasMatch(data.navigateUri)) {
          launchUrl(
            Uri.parse(data.navigateUri),
            mode: LaunchMode.platformDefault,
          );
        }
        if (localUriRef.hasMatch(data.navigateUri)) {
          Get.toNamed(data.navigateUri);
        }
      },
      child: Container(
        width: width,
        height: 100,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(8),
        ),
        padding: const EdgeInsets.all(10),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Expanded(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    data.title,
                    style: const TextStyle(
                      overflow: TextOverflow.ellipsis,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                  Text(
                    data.subTitle,
                    style: const TextStyle(
                      overflow: TextOverflow.ellipsis,
                    ),
                  ),
                ],
              ),
            ),
            CommonImageWidget(
              data.imageUri,
              width: 60,
            ),
          ],
        ),
      ),
    );
  }
}
